﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.mMall.Buyer
{
    public partial class mfChangeTouristInfo : msBsc.Cls.mbPCustom
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void getUserInfo()
        {
            string userId = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(userId))
            {
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizCustomerBaseInfo userInfo = new AppLogic.DataAlc.CBizCustomerBaseInfo())
            {
                DataSet ds = userInfo.GetVisitorByIds(userId);
                if (ds != null)
                {
                    string ret = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 50, "");
                    this.ExecJScript(@"showUserInfo(" + ret + ")");
                }
                string country = ds.Tables[0].Rows[0]["CUSTOMER_COUNTRY_ID"].ToString();
                string city = ds.Tables[0].Rows[0]["CUSTOMER_CITY_ID"].ToString();
                string destion = ds.Tables[0].Rows[0]["CUSTOMER_DISTINATION_ID"].ToString();
                initplace(country, city, destion);
            }
        }

        private void initplace(string countryId, string cityId, string destionId)
        {
            string parentsId = this.GetParamHtValue("parentsId");
            string isAll = this.GetParamHtValue("isAll");
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return;
                }
                if (string.IsNullOrEmpty(countryId))
                {
                    countryId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                string strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"setCity(" + strJson + @",""country_select"",'" + countryId + "');");

                ds = cbcc.SelectCountryByParentId(countryId);
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return;
                }
                if (string.IsNullOrEmpty(cityId))
                {
                    cityId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"setCity(" + strJson + @",""city_select"",'" + cityId + "');");

                ds = cbcc.SelectCountryByParentId(cityId);
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return;
                }
                if (isAll.ToLower() == "true")
                {
                    DataRow dr = ds.Tables[0].NewRow();
                    dr["COUNTRY_CITY_ID"] = "";
                    dr["COUNTRY_CITY_NME"] = "全部";
                    ds.Tables[0].Rows.InsertAt(dr, 0);
                }
                if (string.IsNullOrEmpty(destionId))
                {
                    destionId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"setCity(" + strJson + @",""destion_select"",'" + destionId + "');");
            }
        }

        private void getArea2()
        {
            string parentsId = this.GetParamHtValue("parentsId");
            string cityId = "";
            string destionId = "";
            string isAll = this.GetParamHtValue("isAll");
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return;
                }
                if (string.IsNullOrEmpty(cityId))
                {
                    cityId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                string strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"setCity(" + strJson + @",""city_select"",'" + cityId + "');");

                ds = cbcc.SelectCountryByParentId(cityId);
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return;
                }
                if (isAll.ToLower() == "true")
                {
                    DataRow dr = ds.Tables[0].NewRow();
                    dr["COUNTRY_CITY_ID"] = "";
                    dr["COUNTRY_CITY_NME"] = "全部";
                    ds.Tables[0].Rows.InsertAt(dr, 0);
                }
                if (string.IsNullOrEmpty(destionId))
                {
                    destionId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"setCity(" + strJson + @",""destion_select"",'" + destionId + "');");
            }
        }
        private void getArea3()
        {
            string parentsId = this.GetParamHtValue("parentsId");
            string destionId = "";
            string isAll = this.GetParamHtValue("isAll");
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return;
                }
                if (isAll.ToLower() == "true")
                {
                    DataRow dr = ds.Tables[0].NewRow();
                    dr["COUNTRY_CITY_ID"] = "";
                    dr["COUNTRY_CITY_NME"] = "全部";
                    ds.Tables[0].Rows.InsertAt(dr, 0);
                }
                if (string.IsNullOrEmpty(destionId))
                {
                    destionId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                string strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"setCity(" + strJson + @",""destion_select"",'" + destionId + "');");
            }
        }

        private void saveUserInfo()
        {
            try
            {
                using (msBsc.AppLogic.DataAlc.CBizCustomerBaseInfo userInfo = new AppLogic.DataAlc.CBizCustomerBaseInfo())
                {

                    string userId = msBsc.SvRef.UserInfo.Operator;
                    if (string.IsNullOrEmpty(userId))
                    {
                        this.Alert("请先登录");
                        this.ExecJScript("$('#saveInfo').val('确认修改');");
                        return;
                    }
                    string userPhoto = this.GetParamHtValue("userPhoto");
                    string userNickName = this.GetParamHtValue("userNickName");
                    string mobilePhone = this.GetParamHtValue("mobilePhone");
                    string phone = this.GetParamHtValue("phone");
                    string QQ = this.GetParamHtValue("QQ");
                    string email = this.GetParamHtValue("email");
                    string weChart = this.GetParamHtValue("weChart");
                    string sex = this.GetParamHtValue("sex");
                    string birth = this.GetParamHtValue("birth");
                    string destionId = this.GetParamHtValue("destionId");
                    string cityId = this.GetParamHtValue("cityId");
                    string countryId = this.GetParamHtValue("countryId");
                    string postFileNme = this.GetParamHtValue("postFileNme");
                    if (userPhoto.IndexOf(";base64") > -1)
                    {
                        userPhoto = userPhoto.Split(',')[1];
                        bool blfnmeutf8 = true;
                        using (msBsc.AppLogic.DataAlc.CBizSysFile sysFile = new AppLogic.DataAlc.CBizSysFile())
                        {
                            userPhoto = sysFile.SaveBase64ImgServer(userPhoto, postFileNme, blfnmeutf8);
                        }
                    }

                    string[] ret = userInfo.UpdateCustomer(userId, userNickName, userPhoto, sex, birth, destionId, cityId, countryId, mobilePhone, phone, QQ, weChart, email);
                    if (ret[0] != "TRUE")
                    {
                        this.Alert(ret[1]);
                        this.ExecJScript("$('#saveInfo').val('确认修改');");
                    }
                    else
                    {
                        msBsc.SvRef.UserInfo.doSetAuthCookie(msBsc.SvRef.UserInfo.Operator, msBsc.SvRef.UserInfo.OperType, "", userNickName);
                        this.ExecJScript(@"$('#saveInfo').val('确认修改');window.location.href='/m/mb_TouristInfo.htm'");
                    }
                }
            }
            catch (Exception e)
            {
                this.ExecJScript("$('#saveInfo').val('确认修改');");
                this.Alert(e.Message);
                return;
            }

        }

        private void saveImg()
        {
            string str64 = this.GetParamHtValue("str64");
            string postFileNme = this.GetParamHtValue("postFileNme");
            bool blfnmeutf8 = true;
            try
            {
                using (msBsc.AppLogic.DataAlc.CBizSysFile sysFile = new AppLogic.DataAlc.CBizSysFile())
                {
                    string data = sysFile.SaveImgToServer(str64, postFileNme, "", "", msBsc.SvRef.UserInfo.Operator, "1", "", "", true, "", blfnmeutf8);
                    this.ExecJScript("getImg(" + data + ");$('#uploadImg').val('上 传');");
                }
            }
            catch (Exception e)
            {
                this.Alert(e.Message);
                this.ExecJScript("$('#uploadImg').val('上 传');");
            }

        }

        public override void DoAction()
        {
            if (this.strAction == "init")
            {
                getUserInfo();
            }
            else if (this.strAction == "upCountry")
            {
                getArea2();
            }
            else if (this.strAction == "upCity")
            {
                getArea3();
            }
            else if (this.strAction == "setUserInfo")
            {
                saveUserInfo();
            }
            else if (this.strAction == "doto")
            {
                saveImg();
            }
        }
    }
}